package com.lms.servlet.user;

import com.alibaba.fastjson.JSONArray;
import com.lms.bean.LabWeek;
import com.lms.bean.User;
import com.lms.dao.BaseDao;
import com.lms.dao.apply.ApplyDao;
import com.lms.dao.apply.ApplyDaoImpl;
import com.lms.dao.root.RootDao;
import com.lms.dao.root.RootDaoImpl;
import com.lms.pojo.LabIntervalData;
import com.lms.pojo.LabWeekData;
import com.lms.pojo.UserApplyData;
import com.lms.service.apply.ApplyService;
import com.lms.service.apply.ApplyServiceImpl;
import com.lms.service.lab.LabService;
import com.lms.service.lab.LabServiceImpl;
import com.lms.service.user.UserService;
import com.lms.service.user.UserServiceImpl;
import com.lms.util.Constants;
import com.lms.util.WeixinUtil.WeixinConstants;
import com.lms.util.WeixinUtil.msg.WeixinPushMsg;
import com.lms.util.WeixinUtil.msg.WeixinPushMsgImpl;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ApplyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method!=null){
            if (method.equals("getLabWeekDataByLabName")){
                this.getLabMsgByLabName(req,resp,req.getParameter("labCode"),req.getParameter("week"));
            }else if (method.equals("getApplyMsg")){
                this.getApplyMsg(req,resp);
            }else if (method.equals("getLabIntervalData")){
                this.getIntervalMsg(req,resp,req.getParameter("week"),req.getParameter("interval"));

            }else if (method.equals("apply")){
                this.apply(req,resp,req.getParameter("labCode"),req.getParameter("applyCount"),req.getParameter("reason"),req.getParameter("week"),req.getParameter("interval"));
            }else if (method.equals("cancelApply")){
                this.cancelApply(req,resp,req.getParameter("applyID"));
            }else if (method.equals("getCheckCount")){
                this.getCheckCount(req,resp);
            }else if (method.equals("checked")){
                this.checked(req,resp);
            }else{
                /*url有误，转发至error*/
                resp.sendRedirect(req.getContextPath() + "/error.jsp");
            }
        }
    }

    public void getLabMsgByLabName(HttpServletRequest req, HttpServletResponse resp,String labCode,String week) throws IOException {
        LabService labService = new LabServiceImpl();
        List<LabWeekData> labWeekList=null;
        Map<String,Object> map=new HashMap<>();

        labWeekList=labService.getLabWeekDataByLabName(labCode,week);
        map.put("data",JSONArray.toJSON(labWeekList));

        resp.getOutputStream().write(JSONArray.toJSONString(map).getBytes("UTF-8"));
        resp.setContentType("text/json; charset=UTF-8");
    }
    public void getApplyMsg(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ApplyService userService=new ApplyServiceImpl();
        List<UserApplyData> userApplyDataList=null;
        Map<String,Object> map=new HashMap<>();
        User user=(User)req.getSession().getAttribute(Constants.USER_SESSION);

        if(user!=null){
            userApplyDataList=userService.getUserApplyDataList(user.getUserCode());
            map.put("data",JSONArray.toJSON(userApplyDataList));
            map.put("total",userApplyDataList.size());
        }

        resp.getOutputStream().write(JSONArray.toJSONString(map).getBytes("UTF-8"));
        resp.setContentType("text/json; charset=UTF-8");

    }
    public void getCheckCount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ApplyService applyService=new ApplyServiceImpl();
        Map<String,Object> map=new HashMap<>();
        User user=(User)req.getSession().getAttribute(Constants.USER_SESSION);

        if(user!=null){
            map.put("checkCount",applyService.getCheckCount(user.getUserCode()));
        }

        resp.getOutputStream().write(JSONArray.toJSONString(map).getBytes("UTF-8"));
        resp.setContentType("text/json; charset=UTF-8");

    }
    public void checked(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ApplyService applyService=new ApplyServiceImpl();
        Map<String,Object> map=new HashMap<>();
        User user=(User)req.getSession().getAttribute(Constants.USER_SESSION);

        if(user!=null){
            if (applyService.checked(user.getUserCode()) == true)
                map.put("ok","yes");

            if (applyService.checked(user.getUserCode()) == false)
                map.put("ok","no");

        }

        resp.getOutputStream().write(JSONArray.toJSONString(map).getBytes("UTF-8"));
        resp.setContentType("text/json; charset=UTF-8");

    }

    public void getIntervalMsg(HttpServletRequest req, HttpServletResponse resp,String week,String interval) throws IOException {
        LabService labService = new LabServiceImpl();
        List<LabIntervalData> labIntervalDataList=null;
        Map<String,Object> map=new HashMap<>();

        labIntervalDataList=labService.getLabIntervalData(week,interval);
        map.put("data",JSONArray.toJSON(labIntervalDataList));
        map.put("total",labIntervalDataList.size());

        resp.getOutputStream().write(JSONArray.toJSONString(map).getBytes("UTF-8"));
        resp.setContentType("text/json; charset=UTF-8");
    }
    /*核心方法*/
    public void apply(HttpServletRequest req, HttpServletResponse resp,String labCode,String applyCount,String reason,String week,String interval) throws IOException {
        Map<String,Object> map=new HashMap<>();
        int flag=0;
        ApplyService applyService=new ApplyServiceImpl();
        User user=(User)req.getSession().getAttribute(Constants.USER_SESSION);
        if (user!=null){
            flag=applyService.apply(user.getUserCode(),labCode,applyCount,"".equals(reason.trim())?null:reason.trim(),week,interval);
        }
        if (flag>0){
            map.put("ok","yes");
            /*用户提交了申请审批请求，需要通知管理员处理*/
            WeixinPushMsg weixinPushMsg = new WeixinPushMsgImpl();
            try {
                RootDao rootDao = new RootDaoImpl();
                weixinPushMsg.push_applyMsg(rootDao.getRootOpenID(BaseDao.getConnection()),WeixinConstants.APPLY_TEMPLATE_ID);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else {
            map.put("ok","no");
        }

        resp.getOutputStream().write(JSONArray.toJSONString(map).getBytes("UTF-8"));
        resp.setContentType("text/json; charset=UTF-8");
    }
    public void cancelApply(HttpServletRequest req, HttpServletResponse resp,String applyID) throws IOException {
        Map<String,Object> map=new HashMap<>();
        int flag=0;
        ApplyService applyService=new ApplyServiceImpl();

        flag=applyService.cancelApply(applyID);
        if (flag>0){
            map.put("ok","yes");
        }else {
            map.put("ok","no");
        }

        resp.getOutputStream().write(JSONArray.toJSONString(map).getBytes("UTF-8"));
        resp.setContentType("text/json; charset=UTF-8");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }


}
